#ifndef XNDCHIP_SERIAL_H
#define XNDCHIP_SERIAL_H

#include <QMainWindow>
#include <QtSerialPort/QSerialPort>
#include <QString>
#include <QLabel>
#include "utilities.h"

namespace Ui {
class XNDchip_Serial;
}

class XNDchip_Serial : public QMainWindow
{
    Q_OBJECT

public:
    explicit XNDchip_Serial(QWidget *parent = nullptr);
    ~XNDchip_Serial();

    /* 定义一个枚举类型来表示不同的芯片类型 */
    enum ChipType
    {
        XND2152MD,
        XND2179MD,
        XND3403MD,
        AKM2157,
        Unknown
    };

    void DataLimit();
    void initSattusbar();
    void initPort();
    void exit();
    void portconfig();
    void Read_Instruct();
    void serialport_write(uint8_t *buff,int lenght);
    QByteArray InttoQByteArray(uint8_t buff[], int lenght);
    void display_message(QString str);
    void sleep(int mesc);
    void serial_write_slot();
    void MTP_Bit_Read();
    void RegData_write(QByteArray data, int Bits);
    ChipType getChipType(const QString &chipTypeStr) const;

    void mode_Chang(uint8_t *buf, uint8_t *buf2);

    void XND2152MD_serial_write();
    void XND2152MD_serial_read();
    void XND2152MD_COPY();

    void AKM2157_serial_read();
    void AKM2157_serial_write(uint8_t command);
    void AKM2157_COPY();

private slots:
    void serial_read_slot();

    void on_ClearButton_clicked();

    void on_OpenButton_clicked();

    void on_Copy_Button_clicked();

    void on_Reg_RButton_clicked();

    void on_Reg_WButton_clicked();

    void on_MTP_ReadButton_clicked();

    void on_MTP_WriteButton_clicked();

    void on_REG47_128_clicked();

    void on_REG47_0_clicked();

    void on_REGRead_clicked();

    void on_RamWrite_clicked();

    void on_REG47_128_READ_clicked();

private:
    Ui::XNDchip_Serial *ui;

    /* 申明一个变量 */
    QSerialPort *port;
    Utilities mybps;
    /* 下拉框配置项的文本值 */
    QString str_port;
    QString str_baudrate;
    QString str_databit;
    QString str_checkbit;
    QString str_stopbit;

    /* 串口、按键是否打开 */
    bool portisOpen = false;
    bool ReadOpen = false;          /* MTP寄存器读 */
    bool writeOpen = false;         /* MTP寄存器写 */
    bool Reg_ROpen = false;         /* 修调寄存器读 */
    bool Reg_WOpen = false;         /* 修调寄存器写 */

    /* 状态栏 */
    QLabel *portStatus;     /* 串口状态 */
    QLabel *workStatus;    /* 工作状态 */
    QLabel *curTimeStatus;  /* 当前时间 */

    uint8_t POWER_3V[6] = {0xF0, 0x00, 0x06, 0x00, 0xF6, 0xFF};                         /* 电压切换到3.3V */
    uint8_t POWER_5V[6] = {0xF0, 0x00, 0x06, 0x01, 0xF7, 0xFF};                         /* 电压切换到5V */

    uint8_t RW_MODE[6] = {0xF0, 0x00, 0x06, 0x02, 0xF8, 0xFF};                          /* 读写模式 */
    uint8_t FRE_MODE[6] = {0xF0, 0x00, 0x06, 0x03, 0xF9, 0xFF};                         /* 频率测试模式 */


    /* XND2152MD发送的固定指令 */
    uint8_t XND2152MD_R_W_ENABLE[8] = {0xF0, 0x01, 0x08, 0x13, 0xD0, 0x07, 0xE3, 0xFF};                       /* 向CTRL0寄存器发送0x07 */
    uint8_t XND2152MD_W_Ctr[12] = {0xF0, 0x01, 0x0C, 0x17, 0xD2, 0x24, 0x26, 0x22, 0x20, 0x00, 0x72, 0XFF};   /* 写操作使能*/
    uint8_t XND2152MD_R_Build[9] = {0xF0, 0x01, 0x09, 0x19, 0xD2, 0x01, 0x00, 0xE6, 0xFF};                    /* 生成读取信号 */
    uint8_t XND2152MD_R_Date[7] = {0xF0, 0x01, 0x07, 0x14, 0x93, 0x9F, 0xFF};                                 /* 在控制器CTRL3中读取数据，返回值为1则为1，为0则为0 */
    uint8_t XND2152MD_Read_REG[8] = {0xF0, 0x01, 0x08, 0x16, 0xA0, 0x0E, 0xBD, 0xFF};                         /* 读修调寄存器REG0-REG14值 */
    uint8_t XND2152MD_Reg_Erase[11] = {0xF0, 0x01, 0x0B, 0x18, 0xD2, 0x08, 0x0A, 0x08, 0x00, 0x00, 0xFF};     /* 擦除操作 */
    uint8_t XND2152MD_MTP_Write_Valid[8] = {0xF0, 0x01, 0x08, 0x20, 0xAF, 0xA5, 0x6D, 0xFF};
    int XND2152MD_Reg_bits[14] = {4, 5, 6, 7, 6, 7, 8, 8, 8, 8, 3, 8, 6, 3};                                  /* 寄存器位数 */

    /* XND2179MD发送的固定指令 */
    uint8_t XND2179MD_R_W_ENABLE[8] = {0xF0, 0x06, 0x08, 0x13, 0xE2, 0x03, 0xF6, 0xFF};                       /* 向CTRL2寄存器发送0x03 */
//    uint8_t XND2179MD_W_Ctr[12] = {0xF0, 0x01, 0x0C, 0x17, 0xD2, 0x24, 0x26, 0x22, 0x20, 0x00, 0x72, 0XFF};   /* 写操作使能*/
//    uint8_t XND2179MD_R_Build[9] = {0xF0, 0x01, 0x09, 0x19, 0xD2, 0x01, 0x00, 0xE6, 0xFF};                    /* 生成读取信号 */
//    uint8_t XND2179MD_R_Date[7] = {0xF0, 0x01, 0x07, 0x14, 0x93, 0x9F, 0xFF};                                 /* 在控制器CTRL3中读取数据，返回值为1则为1，为0则为0 */
    uint8_t XND2179MD_Read_REG[8] = {0xF0, 0x06, 0x08, 0x16, 0x80, 0x0D, 0xA1, 0xFF};                         /* 读修调寄存器REG0-REG11值 */
    uint8_t XND2179MD_Reg_Erase[11] = {0xF0, 0x06, 0x0B, 0x14, 0xE3, 0x10, 0x12, 0x10, 0x00, 0x2A, 0xFF};     /* 擦除操作 */
    uint8_t XND2179MD_MTP_Write_Valid[8] = {0xF0, 0x06, 0x08, 0x17, 0x0C, 0xA5, 0xC6, 0xFF};                    /* MTP写完数据后写入0xA5 */
    uint8_t XND2179MD_MTP_Write[8] = {0xF0, 0x06, 0x08, 0x17, 0x00, 0x00, 0x15, 0xFF};                          /* MTP写数据 */

    /* AKM2157发送的固定指令 */
    uint8_t AKM2157_Read_REG[7] = {0xF0, 0x02, 0x07, 0x12, 0x05, 0x10, 0xFF};                          /* 读修调寄存器REG0-REG26值 */
    uint8_t AKM2157_RamRead[7] = {0xF0, 0x02, 0x07, 0x12, 0x04, 0x0F, 0xFF};                           /*读取RAM寄存器的值*/
                                                //   指令，读命令，CRC,帧尾
    uint8_t AKM2157_REG47_Write_128[9] = {0xF0, 0x02, 0x09, 0x13, 0x03, 0x2F, 0x80, 0xC0, 0xFF};                          /* REG47写入0d128 */
//    uint8_t AKM2157_REG47_Write_128[9] = {0xF0, 0x02, 0x09, 0x11, 0x03, 0x2F, 0xFF, 0x3D, 0xFF};                          /* REG47写入0d128 */

    uint8_t AKM2157_REG47_Write_0[9] = {0xF0, 0x02, 0x09, 0x13, 0x03, 0x2F, 0x00, 0x40, 0xFF};                          /* REG47写入0d0 */
    uint8_t AKM2157_REG47_READ[8] = {0xF0, 0x02, 0x07, 0x10, 0x04, 0x2F, 0x3C, 0xFF};

    uint8_t AKM2157_REG25_Write[9] = {0xF0, 0x02, 0x09, 0x13, 0x03, 0x19, 0x00, 0x2A, 0xFF};                          /* REG25写入0d0 */
};

#endif // XNDCHIP_SERIAL_H
